package com.semidata.trp.algorithm.request;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.semidata.rtd.core.json.JsonObject;
import com.semidata.rtd.core.service.ServiceManager;
import com.semidata.trp.decision.business.BusinessService;
import com.semidata.trp.decision.business.Material;
import com.semidata.trp.decision.business.Resource;
import com.semidata.trp.decision.business.Strategy;
import com.semidata.trp.decision.job.DecisionJobContext;
import com.semidata.trp.decision.report.Cost;
import com.semidata.trp.decision.report.ResourceReport;
import com.semidata.trp.decision.report.StrategyReport;

public class StrategyContext {
	private Strategy strategy;
	private DecisionJobContext context;
	private Timestamp timestamp;
	private Cost orgCost;
	private StrategyReport strategyReport;
	private ResourceReport resourceReport;
	private JsonObject params;
	private Resource resource;
	public StrategyContext(Strategy strategy, DecisionJobContext context) {
		this.strategy = strategy;
		this.context = context;
		this.timestamp = new Timestamp(System.currentTimeMillis());
		BusinessService service = ServiceManager.getService(BusinessService.class);
		this.orgCost = service.getReportData().getOrgReport().get(strategy.getOrgId());
		this.strategyReport = service.getReportData().getStrategyReport().get(strategy.getId());
		this.resourceReport = service.getReportData().getResourceReport().get(strategy.getResource().getId());
		this.params = strategy.getAlgoParams();
		this.resource = strategy.getResource();
	}
	
	public void log(String log) {
		
	}
	public Integer getPositionId() {
		return this.context.getRequest().getPositionId();
	}
	public Integer getPositionColumns() {
		return this.strategy.getResource().getPosition().getMax();
	}
	
	public Double getStrategyDailyInternalBudget() {
			return this.strategy.getDailyBudgetIn();
	}
	
	public Double getStrategyTotalInternalBudget() {
			return this.strategy.getTotalBudgetIn();
	}
	
	public Double getStrategyDailyExternalBudget() {
			return this.strategy.getDailyBudgetOut();
	}
	
	public Double getStrategyTotalExternalBudget() {
			return this.strategy.getTotalBudgetOut();
	}
	public Double getStrategyDailyAllyBudget() {
			return this.strategy.getDailyBudgetAlly();
	}
	
	public Double getStrategyTotalAllyBudget() {
			return this.strategy.getTotalBudgetAlly();
	}
	
	public List<String> getMaterialsId() {
		List<String> ids = new ArrayList<String>();
		Map<Material,Integer> materials = this.strategy.getMaterialWeights();
		for (Material material : materials.keySet())
			ids.add(material.getId() + "");
		return ids;
	}
	
	public Integer getHour() {
		return this.timestamp.getHours();
	}
	
	public String getAreaCode() {
		return this.context.getRequest().getArea().toString();
	}
	public JsonObject getParams() {
		return this.params;
	}
	public String getRequestKeyWord1() {
		return this.context.getRequest().getKeyword1();
	}
	
	public String getRequestKeyWord2() {
		return this.context.getRequest().getKeyword2();
	}
	
	public String getRequestKeyWord3() {
		return this.context.getRequest().getKeyword3();
	}
	
	public Map<String, Integer> getMaterialFrequency() {
		return this.context.getFrequency();
	}
	
	
	public Double getOrgTodayInternalCost() {
				
		return orgCost == null ? 0.0 : orgCost.getToday_in();
	}
	
	public Double getOrgTodayExternalCost() {
				
		return orgCost == null ? 0.0 : orgCost.getToday_out();
	}
	
	public Double getOrgTodayAllyCost() {
		return orgCost == null ? 0.0 : orgCost.getToday_ally();
	
	}
	
	public Double getOrgTotalInternalCost() {
		
		return orgCost == null ? 0.0 : orgCost.getTotal_in();
	}
	
	public Double getOrgTotalExternalCost() {
		
		return orgCost == null ? 0.0 : orgCost.getTotal_out();
	}
	
	public Double getOrgTotalAllyCost() {
		
		return orgCost == null ? 0.0 : orgCost.getTotal_ally();
	}
	
	public Long getResourceTodayClick() {
		return resourceReport == null ? 0l : resourceReport.getClick().getToday();
	}
	
	public Long getResourceTotalClick() {
		return resourceReport == null ? 0l : resourceReport.getClick().getTotal();
	}
	
	public Long getResourceTodayImpression() {
		return resourceReport == null ? 0l : resourceReport.getImpr().getToday();
	}
	
	public Long getResourceTotalImpression() {
		return resourceReport == null ? 0l : resourceReport.getImpr().getTotal();
	}
	
	public Long getStrategyTodayClick() {
		return strategyReport == null ? 0l : strategyReport.getClick().getToday();
	}
	
	public Long getStrategyTotalClick() {
		return strategyReport == null ? 0l : strategyReport.getClick().getTotal();
	}
	
	public Long getStrategyTodayInternalImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getToday_in();
	}
	
	public Long getStrategyTodayExternalImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getToday_out();
	}
	
	public Long getStrategyTodayAllyImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getToday_ally();
	}
	
	public Long getStrategyTotalInternalImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getTotal_in();
	}
	
	public Long getStrategyTotalExternalImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getTotal_out();
	}
	
	public Long getStrategyTotalAllyImpression() {
		return strategyReport == null ? 0l : strategyReport.getImpr().getTotal_ally();
	}
	
	public Double getStrategyTodayInternalCost() {
		
		return strategyReport == null ? 0.0 : strategyReport.getCost().getToday_in();
	}
	
	public Double getStrategyTodayExternalCost() {
				
		return strategyReport == null ? 0.0 : strategyReport.getCost().getToday_out();
	}
	
	public Double getStrategyTodayAllyCost() {
		return strategyReport == null ? 0.0 : strategyReport.getCost().getToday_ally();
	
	}
	
	public Double getStrategyTotalInternalCost() {
		
		return strategyReport == null ? 0.0 : strategyReport.getCost().getTotal_in();
	}
	
	public Double getStrategyTotalExternalCost() {
		
		return strategyReport == null ? 0.0 : strategyReport.getCost().getTotal_out();
	}
	
	public Double getStrategyTotalAllyCost() {
		
		return strategyReport == null ? 0.0 : strategyReport.getCost().getTotal_ally();
	}
	
	public Integer getMediaType() {
		return this.strategy.getResource().getPosition().getMediaType();
	}
	
	public Double getResourceFloor() {
		return resource.getFloor();
	}
	
}
